﻿#include "Vec4.h"
#include "../Misc.h"

namespace Base {
	Vec4 Vec4::operator/(float scalar)const {
		if (scalar == 0) {
			Debug::ThrowOrMessageBox("scalar is 0!");
		}
		return Vec4(x / scalar, y / scalar, z / scalar, w / scalar);
	}

	Vec4 Vec4::ParseColor(const char* color) {
		std::string s(color);
		Trim(s);
		if (s[0] != '#')
		{
			Debug::ThrowOrMessageBox("color value error!");
		}
		Vec4 result;
		int len = s.length();
		if (len == 7)
		{
			// #RGB
			result.x = std::stoi(s.substr(1, 2), nullptr, 16) / 255.0f;
			result.y = std::stoi(s.substr(3, 2), nullptr, 16) / 255.0f;
			result.z = std::stoi(s.substr(5, 2), nullptr, 16) / 255.0f;
			result.w = 1;
		}
		else if (len == 9) {
			// #RGBA
			result.x = std::stoi(s.substr(1, 2), nullptr, 16) / 255.0f;
			result.y = std::stoi(s.substr(3, 2), nullptr, 16) / 255.0f;
			result.z = std::stoi(s.substr(5, 2), nullptr, 16) / 255.0f;
			result.w = std::stoi(s.substr(7, 2), nullptr, 16) / 255.0f;
		}
		else {
			Debug::ThrowOrMessageBox("color value error!");
		}
		return result;
	}
}